package com.tianya.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianya.domain.dto.PasswordDTO;
import com.tianya.domain.dto.UserRequestDTO;
import com.tianya.domain.entity.User;
import com.tianya.domain.enums.Message;
import com.tianya.domain.vo.PageResultVO;
import com.tianya.domain.vo.UserVO;
import com.tianya.exception.CustomException;
import com.tianya.service.UserService;
import com.tianya.mapper.UserMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author 86191
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2025-05-24 19:00:17
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Override
    public UserVO getUserInfo(Long id) {
        User user = this.getById(id);
        return UserVO.builder()
                .id(user.getUserId())
                .username(user.getUsername())
                .name(user.getName())
                .phone(user.getPhone())
                .gender(user.getGender())
                .role(user.getRole())
                .status(user.getStatus())
                .avatar(user.getAvatar())
                .createTime(user.getCreatedAt())
                .updateTime(user.getUpdatedAt())
                .build();
    }

    @Override
    public void modifyPassword(PasswordDTO passwordDTO) {
        User user = this.getById(passwordDTO.getId());

        //1.对比密码
        if (!DigestUtils.md5DigestAsHex(passwordDTO.getOldPassword().getBytes()).equals(user.getPassword())) {
            throw new CustomException(Message.USERNAME_PASSWORD_ERROR);
        }

        //2.修改密码
        user.setPassword(DigestUtils.md5DigestAsHex(passwordDTO.getNewPassword().getBytes()));
        this.updateById(user);
    }

    @Override
    public void editUserInfo(Long id, UserVO userVO) {
        User user = this.getById(id);
        user.setName(userVO.getName());
        user.setPhone(userVO.getPhone());
        user.setGender(userVO.getGender());
        user.setRole(userVO.getRole());
        user.setUsername(userVO.getUsername());
        user.setStatus(userVO.getStatus());
        this.updateById(user);
    }

    @Override
    public PageResultVO<UserVO> pageUser(Integer pageNum, Integer pageSize, UserRequestDTO userRequestDTO) {
        Page<User> pageParam = new Page<>(pageNum, pageSize);
        Page<User> pageResult = baseMapper.selectPage(pageParam, null);
        PageResultVO<UserVO> pageVO = new PageResultVO<>();
        pageVO.setTotal(pageResult.getTotal());
        List<UserVO> userVOList = new ArrayList<>();
        for (User user : pageResult.getRecords()) {
            UserVO userVO = UserVO.builder()
                    .id(user.getUserId())
                    .username(user.getUsername())
                    .name(user.getName())
                    .phone(user.getPhone())
                    .gender(user.getGender())
                    .role(user.getRole())
                    .status(user.getStatus())
                    .createTime(user.getCreatedAt())
                    .updateTime(user.getUpdatedAt())
                    .build();
            userVOList.add(userVO);
        }
        pageVO.setRecords(userVOList);
        return pageVO;
    }
}




